<?php
namespace Apipad\Controller;

use Think\Controller;

//use Common\Model\KuerpProductModel as KuerpProduct;

class UserWarehouseController extends CommonController
{
    public $allow_del = 0;//物理删除 0:不允许 1:允许
    public $pk = 'id'; //默认主键
 

    //全部数据树形
    function getTreeList()
    {
        $model = D(CONTROLLER_NAME);
        $map = $this->_search();
        $data = $model->getList($map, 10000, 'pid asc,id asc');
        $tree = new \Think\Tree($data['list']);
        $tree->key_field = 'id'; //主键字段名
        $tree->name_field = 'title'; //主键字段名
        $tree->fid_field = 'pid'; //父节点字段名
        $c_list = $tree->get_tree(0);
        $this->ajaxSucReturn($c_list);
    }

    //全部数据数组
    function getArrList(){
        $model = D(CONTROLLER_NAME);
        $wdata['pid'] = 0;
        $wdata['status'] = I('status') ? : 3;//默认设计师款式
        $list = $model->where($wdata)->select();
        foreach($list as &$val){
            if($val['child_num']>0){
                $val['childs'] = $this->digui($val);
            }
        }
        $this->ajaxSucReturn($list);
    }

    //批量入库
    function batchInto(){
        $request = I('post.');
        $kw_model = D('UserKuerpWarehouse');
        $kw_model->startTrans();//启用事务
        //dump($request);exit;
        $o_model = M('UserKuerpWarehouseOrders');
        $g_model = M('UserGoods');
        foreach($request['nums'] as $key=>$val){
            if(!$request['nums'][$key]){
                //数量批次不存在则跳过
                continue;
            }
            $product_id = $request['product_id'][$key];
            $nums += $request['nums'][$key];
        }
        if(!$product_id){
            $this->ajaxErrReturn('商品不存在');
        }
        $g_wdata['id'] = $product_id;
        $g_vo = $g_model->where($g_wdata)->find();
        if(!$g_vo){
            $this->ajaxErrReturn('商品不存在');
        }
        $o_data['uid'] = UID;
        $o_data['type'] = 1;
        $o_data['product_id'] = $product_id;
        $o_data['goods_name'] = $g_vo['goods_name'];
        $o_data['goods_sn'] = $g_vo['goods_sn'];
        $o_data['goods_attr'] = $g_vo['goods_attr'];
        $o_data['chenfen'] = $g_vo['chenfen'];
        $o_data['unit'] = $g_vo['unit'];
        $o_data['nums'] = $nums;
        $o_data['add_time'] = NOW_TIME;
        $order_id = $o_model->add($o_data);
        if(!$order_id){
            //echo M()->_sql();exit;
            $this->ajaxErrReturn('添加订单失败');
        }

        foreach($request['nums'] as $key=>$val){
            if(!$request['nums'][$key]){
                //数量批次不存在则跳过
                continue;
            }
            $input['uid'] = UID;
            $input['order_id'] = $order_id;
            $input['wh_id'] = $request['wh_id'][$key];
            $input['loc_id'] = $request['loc_id'][$key];
            $input['product_id'] = $request['product_id'][$key];
            $input['item_id'] = $request['item_id'][$key];
            $input['batch_no'] = $request['batch_no'][$key] ? : 1;
            $input['nums'] = $request['nums'][$key];
            $input['remark'] = $request['remark'][$key];
            $result = $kw_model->into($input);
            if(!$result['status']){
                $this->ajaxErrReturn($result['msg']);
            }
            $product_id = $request['product_id'][$key];
            $nums += $request['nums'][$key];
            //dump($result);
        }
        //exit;
        $kw_model->commit();//提交事务
        $this->ajaxSucReturn('入库成功-'.$order_id);
    }

    //批量出库
    function batchOut(){
        $request = I('');
        $kw_model = D('UserKuerpWarehouse');
        $kw_model->startTrans();//启用事务
        //dump($request);
        if(!I('wuliu_no')){
            $this->ajaxSucReturn('物流单号必须');
        }
        $num = 0;
        foreach($request['nums'] as $key=>$val){
            if(!$request['nums'][$key]){
                //数量批次不存在则跳过
                continue;
            }
            if(!is_numeric($request['nums'][$key])){
                $this->ajaxSucReturn('出库数量必须是数字');
            }
            $input['product_id'] = $request['product_id'][$key];
            $input['item_id'] = $request['item_id'][$key];
            if(!$input['product_id']){
                //商品不存在则跳过
                continue;
            }
            $product_id = $request['product_id'][$key];
            $nums += $request['nums'][$key];
            $num++;
        }
        if($num==0){
            $this->ajaxSucReturn('请提交数据');
        }
        $o_model = M('UserKuerpWarehouseOrders');
        $g_model = M('UserGoods');
        $g_wdata['id'] = $product_id;
        $g_vo = $g_model->where($g_wdata)->find();
        $o_data['uid'] = UID;
        $o_data['type'] = 2;
        $o_data['wuliu_no'] = $request['wuliu_no'] ? : '';
        $o_data['product_id'] = $product_id;
        $o_data['goods_name'] = $g_vo['goods_name'];
        $o_data['goods_sn'] = $g_vo['goods_sn'];
        $o_data['goods_attr'] = $g_vo['goods_attr'];
        $o_data['chenfen'] = $g_vo['chenfen'];
        $o_data['unit'] = $g_vo['unit'];
        $o_data['nums'] = $nums;
        $o_data['add_time'] = NOW_TIME;
        $order_id = $o_model->add($o_data);
        if(!$order_id){
            //echo M()->_sql();exit;
            $this->ajaxErrReturn('添加订单失败');
        }
        foreach($request['nums'] as $key=>$val){
            if(!$request['nums'][$key]){
                //数量批次不存在则跳过
                continue;
            }
            $input['product_id'] = $request['product_id'][$key];
            $input['item_id'] = $request['item_id'][$key];
            if(!$input['product_id']){
                //商品不存在则跳过
                continue;
            }
            $input['wuliu_no'] = $request['wuliu_no'] ? : '';
            $input['uid'] = UID;
            $input['order_id'] = $order_id;
            //$input['resource_type'] = $request['resource_type'][$key];
            $input['wh_id'] = $request['wh_id'][$key];
            $input['loc_id'] = $request['loc_id'][$key];
            $input['batch_no'] = $request['batch_no'][$key] ? : 1;
            $input['nums'] = $request['nums'][$key];
            $input['remark'] = $request['remark'][$key];
            $result = $kw_model->out($input);
            if(!$result['status']){
                $this->ajaxErrReturn($result['msg']);
            }
        }
        $kw_model->commit();//提交事务
        $this->ajaxSucReturn('出库成功 - '.$order_id);
    }

    //按仓库获取商品库存列表
    function getWhProductList(){
        //获取仓库商品列表
        $kwlp_model = D('UserKuerpWarehouseLocProduct');
        if(!I('wh_id') && !I('loc_id') && !I('item_id') && !I('product_id')){
            $this->ajaxErrReturn('搜索条件必须存在');
        }
        if(I('wh_id')){
            $kwlp_wdata['wh_id'] = I('wh_id');
        }
        if(I('loc_id')){
            $kwlp_wdata['loc_id'] = I('loc_id');
        }
        if(I('product_id')){
            $kwlp_wdata['product_id'] = I('product_id');
        }
        if(I('item_id')){
            $kwlp_wdata['item_id'] = I('item_id');
        }
        $kwlp_wdata['uid'] = UID;
        $kwlp_list = $kwlp_model->where($kwlp_wdata)->select();
        foreach($kwlp_list as &$val){
            //$val['product_name'] = strip_tags($val['product_name']);
        }
        $this->ajaxSucReturn($kwlp_list);
    }

    //出库入库公用
    function warehousePub(){
        $request = I('');
        //供应商
        $ks_model = M('UserKuerpSuppliers');
        $ks_wdata['status'] = 1;
        $ks_list = $ks_model->where($ks_wdata)->select();
        $this->assign('ks_list',$ks_list);
        //仓库
        $kwh_model = M('UserKuerpWarehouse');
        $kwh_wdata['status'] = 1;
        $kwh_list = $kwh_model->where($kwh_wdata)->select();
        $this->assign('kwh_list',$kwh_list);
        //库位
        $kwl_model = D('UserKuerpWarehouseLocation');
        $kwl_wdata['wh_id'] = $request['wh_id'] ? $request['wh_id'] : $kwh_list[0]['id'];//第一个默认仓位
        $kwl_wdata['status'] = 1;
        $loc_data = $kwl_model->getList($kwl_wdata, 10000);
        //dump($loc_data);echo M()->_sql();exit;
        $this->assign('loc_list',$loc_data['list']);
    }

    //库位商品数据读取
    function getLocProduct(){
        if(!I('loc_id')){
            $this->ajaxErrReturn('库位必须');
        }
        if(I('order_sn')){
            $kwlp_model = D('UserKuerpWarehouseOrder');
            $kwlp_wdata['order_sn'] = I('order_sn');
            $kwlp_wdata['loc_id'] = I('loc_id');
        }else{
            $kwlp_model = D('UserKuerpWarehouseLocProduct');
            $kwlp_wdata['loc_id'] = I('loc_id');
        }
        $data = $kwlp_model->getList($kwlp_wdata);
        foreach($data['list'] as &$val){
            $val['product_name'] = strip_tags($val['product_name']);
        }
        $this->ajaxSucReturn($data);
    }

    //库位读取
    function getLoc(){
        $kwl_model = D('UserKuerpWarehouseLocation');
        $kwl_wdata['status'] = 1;
        if(I('wh_id')){
            $kwl_wdata['wh_id'] = I('wh_id');
        }
        $data = $kwl_model->getList($kwl_wdata);
        $return['status'] = true;
        $return['info'] = '请求成功';
        $return['data'] = $data;
        $this->ajaxReturn($return);
    }

    
}

?>